<?php
class API
{
    private $status = 1;
    private $errors = array();
    private $data = array();
    private $funcParams = array();
    private $config = null;

    public function __construct()
    {
        $this->config = include dirname(__FILE__) . '/../app/config.php';
    }

    protected function addError($error)
    {
        $this->status = 0;
        $this->errors[] = $error;
    }

    public function getResult()
    {
        return array(
            'status' => $this->status,
            'errors' => $this->errors,
            'data' => $this->data
        );
    }

    public function cmd()
    {
        if (!isset($_REQUEST['do']))
        {
            $this->addError('Wrong parameters');
            return false;
        }

        switch ($_REQUEST['do'])
        {
            default:
                $this->addError('Invalid function "' . $_REQUEST['do'] . '"');
                return false;
                break;

            case 'sendImmediately':
                $this->parseRequest(array(
                    'locale'    => true,
                    'name'      => true,
                    'email'     => true,
                    'data'      => true
                ));

                if ($this->status === 1)
                {
                    $message = new Message(
                        $this->funcParams['locale'],
                        $this->funcParams['name'],
                        $this->funcParams['data']
                    );

                    if (!($this->data = $message->send($this->funcParams['email'])))
                    {
                        $this->addError($message->getError());
                    }
                }
                break;

            case 'sendToQueue':
                $this->parseRequest(array(
                    'locale'    => true,
                    'name'      => true,
                    'email'     => true,
                    'data'      => true
                ));

                if ($this->status === 1)
                {
                    $message = new Message(
                        $this->funcParams['locale'],
                        $this->funcParams['name'],
                        $this->funcParams['data']
                    );

                    $this->data = $message->queue($this->funcParams['email']);
                }
                break;

            case 'getTemplateList':
                $this->parseRequest(array(
                    'locale' => true,
                    'sort_field' => false,
                    'sort_order' => false
                ));

                if ($this->status === 1)
                {
                    $this->data = Template::getList(
                        $this->funcParams['locale'],
                        $this->funcParams['sort_field'],
                        $this->funcParams['sort_order']
                    );
                }
                break;

            case 'getTemplate':
                $this->parseRequest(array(
                    'locale'    => true,
                    'name'      => true
                ));

                if ($this->status === 1)
                {
                    $this->data = Template::getOne($this->funcParams['locale'], $this->funcParams['name']);
                }
                break;

            case 'saveTemplate':
                $this->parseRequest(array(
                    'locale'        => true,
                    'name'          => true,
                    'description'   => true,
                    'subject'       => true,
                    'body'          => true
                ));

                if ($this->status === 1)
                {
                    $this->data = Template::save(
                        $this->funcParams['locale'],
                        $this->funcParams['name'],
                        $this->funcParams['description'],
                        $this->funcParams['subject'],
                        $this->funcParams['body']
                    );
                }
                break;

            case 'deleteTemplate':
                $this->parseRequest(array(
                    'locale'    => true,
                    'name'  => true
                ));

                if ($this->status === 1)
                {
                    $this->data = Template::delete(
                        $this->funcParams['locale'],
                        $this->funcParams['name']
                    );
                }
                break;
        }

        return true;
    }

    private function parseRequest($validParams)
    {
        $this->funcParams = array();

        foreach ($validParams as $paramName => $isRequired)
        {
            if (!empty($_REQUEST[$paramName]))
            {
                $this->funcParams[$paramName] = $_REQUEST[$paramName];
            }
            else
            {
                $this->funcParams[$paramName] = false;

                if ($isRequired)
                {
                    $this->addError('Parameter "' . $paramName . '" - is required');
                }
            }
        }
    }
}
